
package net.atremmedia.gwt.actionjava.examples.bounds.samples;

import net.atremmedia.gwt.actionjava.examples.bounds.samples.curve.DrawCurve;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.geom.Point2D;
import actionjava.geom.curve.ArcTanCurve2D;

public class DrawArcToBounds2 extends DrawCurve
{
	public static final String NAME = "Draw arcTo bounds 2";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bounds/src/main/java/net/atremmedia/gwt/actionjava/examples/bounds/samples/DrawArcToBounds2.java";

	private Point2D p1;
	private Point2D p2;
	private Point2D p3;
	private Point2D cp1;
	private Point2D cp2;
	private Point2D cp3;
	private int radius;

	/*
	 * ---------------------------------------------------------------
	 *	Constructor & init
	 * ---------------------------------------------------------------
	 */

	public DrawArcToBounds2(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	private void initialize()
	{
		// Start point
		p1 = new Point2D(getRandomNumber(200, getWidth() - 400), getRandomNumber(200, getHeight() - 400));

		// Control point 1
		cp1 = new Point2D(getRandomNumber(200, getWidth() - 200), getRandomNumber(200, getHeight() - 200));

		// Control point 2
		cp2 = new Point2D(getRandomNumber(200, getWidth() - 200), getRandomNumber(200, getHeight() - 200));

		// Radius
		radius = ((Double) getRandomNumber(25, 50)).intValue();
	}

	/*
	 * ---------------------------------------------------------------
	 *	Abstract Overrides
	 * ---------------------------------------------------------------
	 */

	@Override
	public void run()
	{
		initialize();
		super.run(true);

		addChild(getCurveShape());
	}

	@Override
	public void drawCurve(Graphics graph)
	{
		graph.moveTo(p1.x, p1.y);
		graph.beginStroke(CURVE_LINE_COLOR);
		graph.arcTo(cp1.x, cp1.y, cp2.x, cp2.y, radius);
		graph.endStroke();
	}

	@Override
	public void drawDots(Graphics graph)
	{
		// P1 dot
		drawDot(p1.x, p1.y, P_DOT_COLOR, graph);

		// Start point
		drawDot(p2.x, p2.y, P1_DOT_COLOR, graph);

		// End point
		drawDot(p3.x, p3.y, P2_DOT_COLOR, graph);

		// CP1 dot
		drawDot(cp1.x, cp1.y, CP_DOT_COLOR, graph);

		// CP2 dot
		drawDot(cp2.x, cp2.y, CP_DOT_COLOR, graph);

		// CENTER dot
		drawDot(cp3.x, cp3.y, P_DOT_COLOR, graph);
	}

	@Override
	public void drawControlPointLines(Graphics graph)
	{
	}

	/*
	 * ---------------------------------------------------------------
	 *	Getters & Setters
	 * ---------------------------------------------------------------
	 */

	@Override
	public void setBounds()
	{
		// ArcTo curve bounds (Rectangle)
		ArcTanCurve2D arcTo = new ArcTanCurve2D(p1, cp1, cp2, radius);
		p2 = arcTo.getArcStartPoint();
		p3 = arcTo.getArcEndPoint();
		cp3 = new Point2D(arcTo.x, arcTo.y);

		super.setBounds(arcTo.getBounds());
	}
}
